package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.ScholarshipMapper;
import com.rivulet.foreign.mapper.UserMapper;
import com.rivulet.foreign.mapper.VacationMapper;
import com.rivulet.foreign.pojo.User;
import com.rivulet.foreign.service.RootService;
import com.rivulet.foreign.tools.Results;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.rivulet.foreign.tools.Identity.*;

/**
 * {ROOT用户服务}
 *
 * @author xujiac
 * @create 2021/7/23
 * @since 1.0.0
 */
@Service
public class RootServiceImpl implements RootService {
    final
    UserMapper userMapper;
    final ScholarshipMapper scholarshipMapper;
    final VacationMapper vacationMapper;

    public RootServiceImpl(UserMapper userMapper, ScholarshipMapper scholarshipMapper, VacationMapper vacationMapper) {
        this.userMapper = userMapper;
        this.scholarshipMapper = scholarshipMapper;
        this.vacationMapper = vacationMapper;
    }



    @Override
    public Results addUser(User user) {
        Results<Object> results = new Results(404, null);
        try {
            if (user != null) {
                userMapper.setUser(user.getUser_id(),user.getPassword(),user.getIdentity());
                if (user.getIdentity() == 1) {
                    userMapper.setUserInformation(user.getUser_id());
                }
                results.setCode(100);
                results.setMsg("用户创建完成，初始密码为" + user.getPassword());
            }
        } catch (Exception e) {
            results.setMsg("创建失败！");
            System.out.println(e.toString());
        }
        return results;
    }

    @Override
    public Results checkUsers(String user_id) {
        Results<List<User>> results = new Results(404, null);
        try {
            if (user_id != null) {
                if (userMapper.getUserById(user_id).getIdentity().equals(ROOT)) {
                    List<User> userList = userMapper.getUsers();
                    results.setCode(100);
                    results.setMsg("查询成功！");
                    results.setData(userList);
                } else {
                    results.setMsg("权限不足！");
                }
            } else {
                results.setMsg("登录异常，请重新登录！");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return results;
    }

    @Override
    public Results delUser(String[] del_users) {
        Results<List<User>> results = new Results(404, null);
        try {
            if (del_users != null) {
                for(String user_id:del_users){
                    User user=userMapper.getUserById(user_id);
                    if(user.getIdentity().equals(ROOT)){
                        results.setMsg("你无法删除管理员账户！！！");
                        return results;
                    }
                    if(user.getIdentity().equals(TEACHER)){
                        userMapper.delUser(user_id);
                    }
                    if(user.getIdentity().equals(STUDENT)) {
                        if(userMapper.getUserInformationById(user_id)!=null){
                            if(scholarshipMapper.getScholarshipById(user_id)!=null){
                                scholarshipMapper.delScholarship(user_id);
                            }
                            if(vacationMapper.getVacationById(user_id)!=null){
                                vacationMapper.delVacation(user_id);
                            }
                            if(userMapper.getUserInformationById(user_id)!=null){
                                userMapper.deleteUserInformation(user_id);
                            }
                            userMapper.delUser(user_id);
                        }
                    }
                }
                results.setCode(100);
                results.setMsg("删除完成！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            results.setMsg("删除异常，请刷新重试");
        }
        return results;
    }

    @Override
    public Results reSetUser(User user) {
        Results results=new Results(400,null);
        try {
            userMapper.reSetPassword("123",user.getUser_id());
            results.setCode(100);
            results.setMsg("密码重置完成!为123");
        }catch (Exception e){
            e.printStackTrace();
        }
        return results;
    }

    @Override
    public Results searchUser(User user) {
        Results results=new Results(400,null);
        try {
            if(user.getUser_id()!=null&&!user.getUser_id().equals("")){
                if(user.getIdentity()!=null&&user.getIdentity()>0){
                    results.setData(userMapper.getUsersLike(user.getUser_id(),user.getIdentity()));
                }
                else {
                    results.setData(userMapper.getUsersLikeId(user.getUser_id()));
                }
            }
            else {
                if(user.getIdentity()!=null&&user.getIdentity()>0){
                    results.setData(userMapper.getUsersLikeIdentity(user.getIdentity()));
                }else {
                    results.setData(userMapper.getUsers());
                }
            }
            results.setCode(100);
        }catch (Exception e){
            e.printStackTrace();
        }
        return results;
    }
}
